#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Mock Redis connection structure (for testing without hiredis)
typedef struct {
    int connected;
    char *hostname;
    int port;
} RedisClient;

// Mock Redis connection function
RedisClient* redis_connect(const char *hostname, int port) {
    RedisClient *client = (RedisClient*)malloc(sizeof(RedisClient));
    if (client) {
        client->connected = 1;
        client->hostname = strdup(hostname);
        client->port = port;
        printf("Mock Redis: Connected to %s:%d\n", hostname, port);
    }
    return client;
}

// Mock Redis disconnect function
void redis_disconnect(RedisClient *client) {
    if (client) {
        if (client->hostname) {
            free(client->hostname);
        }
        free(client);
        printf("Mock Redis: Disconnected\n");
    }
}

// Mock Redis SET function
int redis_set(RedisClient *client, const char *key, const char *value) {
    if (!client || !client->connected) return -1;
    printf("Mock Redis: SET %s = %s\n", key, value);
    return 0; // Success
}

// Mock Redis GET function
char* redis_get(RedisClient *client, const char *key) {
    if (!client || !client->connected) return NULL;
    printf("Mock Redis: GET %s\n", key);
    // Return a mock value for testing
    return strdup("mock_value");
}

// Mock Redis HSET function
int redis_hset(RedisClient *client, const char *hash, const char *field, const char *value) {
    if (!client || !client->connected) return -1;
    printf("Mock Redis: HSET %s %s = %s\n", hash, field, value);
    return 0; // Success
}

// Mock Redis HGET function
char* redis_hget(RedisClient *client, const char *hash, const char *field) {
    if (!client || !client->connected) return NULL;
    printf("Mock Redis: HGET %s %s\n", hash, field);
    // Return a mock value for testing
    return strdup("mock_hash_value");
}

// Mock Redis LPUSH function
int redis_lpush(RedisClient *client, const char *list, const char *value) {
    if (!client || !client->connected) return -1;
    printf("Mock Redis: LPUSH %s %s\n", list, value);
    return 0; // Success
}

// Mock Redis RPUSH function
int redis_rpush(RedisClient *client, const char *list, const char *value) {
    if (!client || !client->connected) return -1;
    printf("Mock Redis: RPUSH %s %s\n", list, value);
    return 0; // Success
}

// Mock Redis DEL function
int redis_del(RedisClient *client, const char *key) {
    if (!client || !client->connected) return -1;
    printf("Mock Redis: DEL %s\n", key);
    return 0; // Success
}

// Mock Redis TTL function
int redis_ttl(RedisClient *client, const char *key) {
    if (!client || !client->connected) return -2;
    printf("Mock Redis: TTL %s\n", key);
    return 3600; // Return 1 hour TTL for testing
}

// Test function to demonstrate the mock Redis functionality
void test_mock_redis() {
    printf("=== Testing Mock Redis Implementation ===\n");
    
    // Connect to mock Redis
    RedisClient *client = redis_connect("127.0.0.1", 6379);
    if (!client) {
        printf("Failed to connect to mock Redis\n");
        return;
    }
    
    // Test basic operations
    printf("\n--- Testing Basic Operations ---\n");
    
    // Test SET/GET
    if (redis_set(client, "test_key", "hello redis") == 0) {
        printf("✓ SET operation successful\n");
        char *value = redis_get(client, "test_key");
        if (value) {
            printf("✓ GET operation successful: %s\n", value);
            free(value);
        }
    }
    
    // Test Hash operations
    printf("\n--- Testing Hash Operations ---\n");
    if (redis_hset(client, "myhash", "field1", "value1") == 0) {
        printf("✓ HSET operation successful\n");
        char *hval = redis_hget(client, "myhash", "field1");
        if (hval) {
            printf("✓ HGET operation successful: %s\n", hval);
            free(hval);
        }
    }
    
    // Test List operations
    printf("\n--- Testing List Operations ---\n");
    if (redis_lpush(client, "mylist", "item1") == 0) {
        printf("✓ LPUSH operation successful\n");
    }
    if (redis_rpush(client, "mylist", "item2") == 0) {
        printf("✓ RPUSH operation successful\n");
    }
    
    // Test other operations
    printf("\n--- Testing Other Operations ---\n");
    redis_del(client, "test_key");
    printf("✓ DEL operation successful\n");
    
    int ttl = redis_ttl(client, "test_key");
    printf("✓ TTL operation successful: %d seconds\n", ttl);
    
    // Disconnect
    redis_disconnect(client);
    printf("\n=== Mock Redis Test Completed ===\n");
}

// Main function for testing
int main() {
    test_mock_redis();
    printf("this is redis test");
    return 0;
} 